/*
 *  Copyright 2019-2025 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.ibmeeting.rest;

import cn.hutool.core.util.RandomUtil;
import com.ibmeeting.annotation.Log;
import com.ibmeeting.domain.EmailConfig;
import com.ibmeeting.domain.dto.EmailDto;
import com.ibmeeting.domain.dto.SmsTemplateRequest;
import com.ibmeeting.service.EmailService;
import com.ibmeeting.service.SmsService;
import com.ibmeeting.utils.RedisUtils;
import com.ibmeeting.utils.constant.CacheConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.dromara.sms4j.core.factory.SmsFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 发送邮件
 * @author 郑杰
 * @date 2018/09/28 6:55:53
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("api/sms")
@Api(tags = "工具：短信服务管理")
public class SmsController {

    private final SmsService smsService;
    private final RedisUtils redisUtils;

    /**
     * 发送验证码接口
     * @param phone 手机号
     * @return 操作结果
     */
    @PostMapping("/send-code")
    public ResponseEntity<Map<String, Object>> sendCode(@RequestParam String configId, @RequestParam String phone) {
        // 获取key
        String key = CacheConstants.CAPTCHA_CODE_KEY+ phone;

     // 生成4位数或6位数的验证码
        String code = RandomUtil.randomNumbers(6);


        // 验证码有效期设置为2分钟
//        RedisUtils redisUtils = new RedisUtils();
        redisUtils.set(key, code, CacheConstants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);

        boolean success = smsService.sendVerificationCode(configId, phone, code);

        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("code", success ? code : null);
        return success ? ResponseEntity.ok(response)
                : ResponseEntity.status(500).body(response);
    }





    /**
     * 发送模板短信接口
     * @param request 包含手机号、模板ID和参数
     * @return 操作结果
     */
    @PostMapping("/send-template")
    public ResponseEntity<?> sendTemplate(@RequestBody SmsTemplateRequest request) {
        boolean success = smsService.sendTemplateMessage(
                request.getPhone(),
                request.getTemplateId(),
                request.getParams()
        );
        return success ? ResponseEntity.ok().build()
                : ResponseEntity.internalServerError().build();
    }

    @PostMapping("/refresh-config")
    public String refreshConfig() {
        SmsFactory.refreshSqlConfig(); // 强制刷新数据库配置
        return "配置已刷新";
    }
}
